package com.cgbl.cg.vm.home

import android.app.Application
import api.ProjectServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.BannerBean
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.HomeProjectBean
import com.robot.base.model.TypeListBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class HomeProjectViewModel(application: Application) : GLBaseViewModel(application) {

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val tabData: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val refreshEvent: SingleLiveEvent<Int> = SingleLiveEvent()
        val ribbonDataEvent = SingleLiveEvent<List<BannerBean>>()
        val homeData = SingleLiveEvent<HomeProjectBean>()
    }

    fun initData() {
        uc.tabData.postValue(arrayListOf("虚拟仿真", "游戏开发", "硬件交互开发", "可视化开发", "影视动画"))
    }

    val toProjectMenuCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.PROJECT.A_PROJECT_MENU)
    })

    val toReleaseProjectCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.PROJECT.A_RELEASE_PROJECT)
    })

    fun refresh() {
        getRibbonList()
        homeData()
    }

    fun getRibbonList() {
        UserServiceFactory.getBannerList(
            3
        ).subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this) {
            override fun onNext(t: BaseResponse<List<BannerBean>>) {
                uc.ribbonDataEvent.postValue(t.data)
            }
        })
    }

    fun homeData() {
        ProjectServiceFactory.homeDate()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<HomeProjectBean>>(this) {
                override fun onNext(t: BaseResponse<HomeProjectBean>) {
                    uc.refreshEvent.postValue(0)
                    uc.homeData.postValue(t.data)
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(0)
                }
            })
    }

    fun allType() {
        ProjectServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {

                }
            })
    }


}